package cn.xianyang.system.controller;

import cn.xianyang.basic.util.AjaxResult;
import cn.xianyang.basic.util.PageList;
import cn.xianyang.system.domain.Systemdictionarytype;
import cn.xianyang.system.query.SystemdictionarytypeQuery;
import cn.xianyang.system.service.ISystemdictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class SystemdictionarytypeController {

    @Autowired
    private ISystemdictionarytypeService service;

    /** 获取全部数据 */
    @GetMapping
    public List<Systemdictionarytype> queryAll(){
        return service.queryAll();
    }

    /** 根据id获取 */
    @GetMapping("/{id}")
    public Systemdictionarytype queryById(@PathVariable Long id){
        return service.queryById(id);
    }

    /** 高级查询分页 */
    @PostMapping
    public PageList<Systemdictionarytype> queryPage(@RequestBody SystemdictionarytypeQuery systemdictionarytypeQuery){
        return service.queryPage(systemdictionarytypeQuery);
    }

    /** 批量删除 */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            service.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }

    /** 根据id删除 */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Long id){
        try {
            service.removeById(id);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }

    /** 新增或修改 */
    @PutMapping
    public AjaxResult AddOrUpdate(@RequestBody Systemdictionarytype systemdictionarytype){
        try {
            if(systemdictionarytype.getId() != null){
                service.update(systemdictionarytype);
            }else{
                service.save(systemdictionarytype);
            }
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }

}